/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package EntityGameObject;

import Supporter.GlobalVariables;
import javax.microedition.lcdui.game.Sprite;
import java.util.Random;

import java.util.Vector;
import javax.microedition.lcdui.Image;

/**
 *
 * @author Khoa
 */
public class Insect extends Sprite {

    public static final int WIDTH = 30;
    public static final int HEIGHT = 39;
    
    public static final int TYPE_MOSQUITO = 1;
    public static final int TYPE_BEE = 2;
    private int level;
    private int type;
    private int speed;
    private boolean isCollision;
    public boolean isFromLeft = false;
    private int yCoordinate;
    private Image imageMos;
    private Image imageBee;

    public Insect(Image imageMos, Image imageBee, boolean fromLeft, int type, int level, int displayHeight) {
        super(imageMos, WIDTH, HEIGHT);
        if (type == TYPE_BEE) {
            setImage(imageBee, WIDTH, HEIGHT);
        }
        this.imageMos = imageMos; // Mos
        this.imageBee = imageBee;
        this.isFromLeft = fromLeft;
        this.level = level;
        this.type = type;
        this.speed = level;
        this.yCoordinate = displayHeight - WIDTH - 2;
        if (!isFromLeft) {
            setTransform(TRANS_MIRROR);
        }
        setVisible(false);
        this.isCollision = false;
      //  this.s


    }

    public void Update(int gameTime, int currentLeftBound, int currentRightBound) {
        if (isIsCollision()) {
            return;
        }
        if ((getRefPixelX() + WIDTH <= currentLeftBound)
                || (getRefPixelX() - WIDTH >= currentRightBound)) {
            setVisible(false);
        }

        int levelStage = (gameTime / 400) + 1;
        if (levelStage > 4)
            levelStage = 4;
        
        int PerAppearMos = 20 * levelStage;
        int PerAppearBee = 200 / levelStage;
      
      
        
        int posMosBasic = 160 - levelStage * 10;
               
        int PosBeeBasic = 30 + levelStage * 15;
        

        int MosPos = 230 - posMosBasic - levelStage * 15;
        int BeePos = levelStage * 30  ;
        
        

        int InsectPerAppear = 0;
        int InsectPos = 0;
        int InsectPosBasic = 0;


        if (getType() == TYPE_MOSQUITO) {
            InsectPerAppear = PerAppearMos;
            InsectPos = MosPos;
            InsectPosBasic = posMosBasic;
            if (gameTime % 400 == 199) {
                speed++;
            }
            if (speed > 5)
                speed = 4;
            if (gameTime > 3000)
                speed = 6;
        }
        if (getType() == TYPE_BEE) {
            InsectPerAppear = PerAppearBee;
            InsectPos = BeePos;
            InsectPosBasic = PosBeeBasic;

        }

        // Di chuyen cua no
        if (!isVisible()) {
            int rand;
            rand = getRandomInt(InsectPerAppear);
            if (rand == 3) {
                setVisible(true);

                yCoordinate = InsectPosBasic + getRandomInt(InsectPos);
                if (type == TYPE_MOSQUITO)    
                {
                    int rand1 = getRandomInt(100);
                    if (rand1 == 4)
                    {
                        yCoordinate = 210;
                    }
                }
                if (type == TYPE_BEE)
                        setImage(imageBee, WIDTH, HEIGHT);
                if (isFromLeft) {
                    
                    setRefPixelPosition(currentRightBound, yCoordinate);
                    move(-1, 0);
                } else {
                    setRefPixelPosition(currentLeftBound, yCoordinate);
                    move(1, 0);
                }

            }
        } else {
            if (gameTime % 5 == 0) {
                nextFrame();
            }

            if (isFromLeft) {
                move(-speed, 0);

            } else {
                move(speed, 0);
            }

        }
    }

    public int getRandomInt(int upper) {

        int retVal = GlobalVariables.myRandom.nextInt() % upper;
        if (retVal < 0) {
            retVal += upper;
        }
        return (retVal);
    }

    public void reset() {
        setVisible(false);

    }

    public void debug() {
        System.out.print(getRefPixelX() + " ");
        System.out.print(getRefPixelY() + " ");
        System.out.println(isVisible());
    }

    /**
     * @return the isCollision
     */
    public boolean isIsCollision() {
        return isCollision;
    }

    /**
     * @param isCollision the isCollision to set
     */
    public void setIsCollision(boolean isCollision) {
        this.isCollision = isCollision;
    }

    public int GetScore() {
        if (getType() == 1) {
            return 40;
        }
        if (getType() == 2) {
            return -100;
        }
        return -1;

    }

    /**
     * @return the imageMos
     */
    public Image getImageMos() {
        return imageMos;
    }

    /**
     * @param imageMos the imageMos to set
     */
    public void setImageMos(Image imageMos) {
        this.imageMos = imageMos;
    }

    /**
     * @return the imageBee
     */
    public Image getImageBee() {
        return imageBee;
    }

    /**
     * @param imageBee the imageBee to set
     */
    public void setImageBee(Image imageBee) {
        this.imageBee = imageBee;
    }

    /**
     * @return the type
     */
    public int getType() {
        return type;
    }

    /**
     * @param type the type to set
     */
    public void setType(int type) {
        this.type = type;
    }
}
